<template>
	<view class="">
		<cu-custom bgColor="bg-gradual-green" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">训练方案</block>
		</cu-custom>

		<view style="width: 100%;position: relative;">
			<image style="width: 100%;height: 260upx;" src="../../static/37.png" mode=""></image>
			<view style="position: absolute;top: 100upx;width: 100%;text-align: center;">
				<view style="color: #fff;">
					添加训练计划,可以更好地定制适合您的训练
				</view>
				<view @click="jump" data-src='/pagesA/Training_program/Create_scenario' style="color: #fff;background-color: #408CE2;width: 40%;height: 60upx;line-height: 60upx;border-radius: 30upx;margin-left: 30%;margin-top: 30upx;font-size: 22upx;">
					+ 添加训练方案
				</view>
			</view>
		</view>




		<uni-swipe-action>
			<uni-swipe-action-item v-for="(item,index) in list" :options="item.options" :key="item.id" @change="swipeChange"
			 @click="swipeClick($event, item.id)">
				<view @click="jump" :data-src='"/pagesA/Training_program/report?id="+item.id' style="width: 100%;background-color: #fff;padding: 20upx;">
					<view style="height: 50upx;display: flex;justify-content: space-between;">
						<view class="title_name">
							{{item.name}}
						</view>
						<view style="font-size: 22upx;color: #666;display: flex;line-height: 40upx;">
							<view style="padding: 0 10upx;background-color: #EFEFEF;line-height: 40upx;height: 40upx;">
								教练
							</view>
							<view style="margin-left: 30upx;">{{item.memberName}}</view>
							<view style="margin-left: 30upx;">{{item.createTime}}</view>
						</view>
					</view>
					<view style="height: 50upx;">
						<view style="font-size: 22upx;color: #666;">
							<text>频率{{item.trainingFrequency}}次/周</text> <text style="margin-left: 30upx;">周期{{item.trainingCycle}}周</text>
							<text style="margin-left: 30upx;">次数{{item.totalNum}}次</text>
						</view>
					</view>
				</view>
			</uni-swipe-action-item>
		</uni-swipe-action>
	</view>
</template>

<script>
	import uniSwipeAction from "../../components/uni-swipe-action/uni-swipe-action.vue"
	import uniSwipeActionItem from "../../components/uni-swipe-action-item/uni-swipe-action-item.vue"
	export default {
		components: {
			uniSwipeAction,
			uniSwipeActionItem,
		},
		data() {
			return {
				list: [],
			};
		},
		onLoad() {
			// this.getData()
		},
		onShow() {
			this.getData()
		},
		methods: {
			async getData() {
				let {
					data: {
						datas
					}
				} = await this.$myRuquest({
					// baseUrl: "http://192.168.1.102:9091",
					url: "/coach/training/getTrainingProgramList",
					data: {
						userId: uni.getStorageSync("phyUser").userId,
						// userId: 285,
					},
				})
				console.log(datas)
				this.list = datas;
				let siwpers = {
					options: [{
						text: '删除',
						style: {
							backgroundColor: 'rgb(255,58,49)'
						}
					}],
					sid: 0,
					content: 'item1'
				}
				this.list.forEach((item, index) => {
					Object.assign(item, siwpers)
				})
			},

			jump(e) {
				console.log(e.currentTarget.dataset.src)
				if (e.currentTarget.dataset.src == 'Not-yet-open') {
					uni.showModal({
						title: '提示',
						content: '功能即将开放',
						showCancel: false,
						confirmColor: "#DD524D",
						success() {
							return
						}
					})
				} else {
					uni.navigateTo({
						url: e.currentTarget.dataset.src
					});
				}
			},
			swipeChange(e) {
				console.log('返回：', e);
			},
			swipeClick(e, id) {
				let {
					content
				} = e
				if (content.text === '删除') {
					uni.showModal({
						title: '提示',
						content: '是否删除',
						success: (res) => {
							console.log('用户点击了删除', res, id)
							this.deleteServer(id)
							if (res.confirm) {} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			},
			// 删除某项
			async deleteServer(trainingProgramId) {
				uni.showLoading({
					title: "正在删除..."
				})
				let {
					data: {
						result
					}
				} = await this.$myRuquest({
					url: "/coach/training/deleteTrainingProgram?trainingProgramId=" + trainingProgramId,
					method: "DELETE",
				})
				if (result == 200) {
					uni.showToast({
						title: '删除成功~',
						icon: "success",
						duration: 1000
					});
					this.getData();
				}
				uni.hideLoading();
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F8F8F8;
	}

	.title_name {
		font-size: 28upx;
		font-weight: bold;
		max-width: 350rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
</style>
